package tw;

class Pendulum extends rk4 {
	public double energy;
	public double length;
	public double theta0;
	private double g;
	private double K;
	int cnt;
	double period;
	double period2;
	int sign;
	int sign2;
	double xc;
	double yc;

	Pendulum(double d) {
		cnt = 50;
		init(2, true);
		g = d;
	}

	public void derivs(double d, double ad[], double ad1[]) {
		ad1[0] = ad[1];
		ad1[1] = K * Math.sin(ad[0]);
	}

	public double move(double d) {
		period2 = 0.0D;
		d /= cnt;
		for (int i = 0; i < cnt; i++) {
			period += d;
			moveNext(d);
			if (super.y[1] > 0.0D)
				sign = 1;
			else
				sign = -1;
			if (sign != sign2) {
				period2 = period;
				sign2 = sign;
				period = 0.0D;
			}
		}

		xc = (int) (length * Math.sin(super.y[0]));
		yc = (int) (length * Math.cos(super.y[0]));
		if (period2 > 0.0D)
			return period2;
		else
			return -period;
	}

	void init(double d, double d1) {
		init(length, d, d1);
	}

	void init(double d, double d1, double d2) {
		theta0 = d1;
		super.y[0] = d1;
		super.y[1] = d2;
		length = d;
		setG(g);
		xc = length * Math.sin(super.y[0]);
		yc = length * Math.cos(super.y[0]);
		period = 0.0D;
		if (super.y[1] > 0.0D)
			sign = 1;
		else
			sign = -1;
		sign2 = sign;
	}

	void setG(double d) {
		g = d;
		K = -g / length;
		energy = g * length * (1.0D - Math.cos(super.y[0]));
	}
}
